home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / isc-src / util / misc / pager.c next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  2.3 KB  |  132 lines

  1. #include <ctype.h>
  2. #include <curses.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <term.h>
  6.  
  7. extern void exit();
  8. extern void perror();
  9.  
  10. #define PROMPT "*** Page Pause, press RETURN to continue, Q to quit: "
  11. #define TTYFILE "/dev/tty"
  12.  
  13. /*---------------------------------------------------------------------------*/
  14.  
  15. static int  done()
  16. {
  17.   resetterm();
  18.   exit(0);
  19. }
  20.  
  21. /*---------------------------------------------------------------------------*/
  22.  
  23. main(argc, argv)
  24. int  argc;
  25. char  **argv;
  26. {
  27.  
  28.   FILE * fi = (FILE * ) 0;
  29.   FILE * fp = stdin;
  30.   char  buf1[1024], buf2[1024];
  31.   int  line = 0;
  32.   int  col = 0;
  33.  
  34.   int  line_not_empty = 0;
  35.   int  maxline;
  36.  
  37.   int  extmaxlines = 22;
  38.   int  extmaxcols  = 255;
  39.   int  i;
  40.  
  41.   register char  *p, *q;
  42.   register int  c;
  43.  
  44.   if (isatty(1) && !(fi = fopen(TTYFILE, "r")))
  45.   {
  46.     perror(TTYFILE);
  47.     exit(1);
  48.   }
  49.  
  50.   if (argc >= 2 && !(fp = fopen(argv[1], "r")))
  51.   {
  52.     perror(argv[1]);
  53.     exit(1);
  54.   }
  55.  
  56.   if (argc > 2)
  57.   {
  58.      extmaxlines = atoi(argv[2]);
  59.   }
  60.  
  61.   if (argc > 3)
  62.   {
  63.      extmaxcols  = atoi(argv[3]);
  64.   }
  65.  
  66.   setupterm(0, 1, 0);
  67.   signal(SIGINT, done);
  68.   signal(SIGQUIT, done);
  69.   signal(SIGTERM, done);
  70.   if ((maxline = lines - 2) < 1) maxline = extmaxlines;
  71.   while (fgets(buf1, sizeof(buf1), fp))
  72.   {
  73.     if (fi && line >= maxline)
  74.     {
  75.       printf("%s%s%s", enter_standout_mode, PROMPT, exit_standout_mode);
  76.       if (!fgets(buf2, sizeof(buf2), fi) || *buf2 == 'q' || *buf2 == 'Q') done();
  77.       line = 0;
  78.     }
  79.  
  80.     /***** remove backspaces *****/
  81.  
  82.     for (p = q = buf1; c = *p++ = *q++; )
  83.       if (c == '\b' && (p -= 2) < buf1) p = buf1;
  84.  
  85.     /***** remove trailing white space *****/
  86.  
  87.     for (p = buf1; *p; p++) ;
  88.     while (--p >= buf1 && isspace(*p & 0xff)) ;
  89.     p[1] = '\0';
  90.  
  91.     /***** expand all tabs *****/
  92.  
  93.     for (p = buf1, q = buf2; *p; )
  94.       if (*p == '\t')
  95.       {
  96.         do
  97.           *q++ = ' ';
  98.         while ((q - buf2) & 7);
  99.         p++;
  100.       }
  101.       else
  102.         *q++ = *p++;
  103.     *q = '\0';
  104.  
  105.     /***** output line *****/
  106.  
  107.     if (*buf2 || line_not_empty)
  108.     {
  109.       col = 0;
  110.       i = 0;
  111.       while (buf2[col] != '\0')
  112.       {
  113.          i++;
  114.          putchar(buf2[col]);
  115.          col++;
  116.          if (i >= extmaxcols)
  117.          {
  118.             i = 0;
  119.             line++;
  120.             putchar('\n');
  121.          }
  122.       }
  123.       line++;
  124.       putchar('\n');
  125.     }
  126.     line_not_empty = *buf2;
  127.   }
  128.   done();
  129.   return 0;
  130. }
  131.  
  132.